<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="著作名称" prop="patentName">
        <el-input
          v-model="queryParams.patentName"
          placeholder="请输入著作名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属单位" prop="affiliatedUnit">
        <el-input
          v-model="queryParams.affiliatedUnit"
          placeholder="请输入所属单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版单位" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入出版单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['work:patent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['work:patent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['work:patent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['work:patent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="patentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="著作名称" align="center" prop="patentName">
        <template slot-scope="scope">
          <el-link style="color: #371ce3" @click="clickDetail(scope.row)">{{scope.row.patentName}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="所属单位" align="center" prop="affiliatedUnit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_unit" :value="scope.row.affiliatedUnit"/>
        </template>
      </el-table-column>
      <el-table-column label="参编作者" align="center" prop="authorId" />
      <el-table-column label="出版时间" align="center" prop="publicationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publicationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="著作类别" align="center" prop="workCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_ategory" :value="scope.row.workCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="出版单位" align="center" prop="publisher" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.school_audit_status" :value="scope.row.auditStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['work:patent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['work:patent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="openDetail" width="1200px" append-to-body>
      <patent-detail :id="patentId" v-if="openDetail">

      </patent-detail>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改获奖对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <AddPatent @handleClose="handleClose" v-if="open" :query-params="form" :type="openClick"></AddPatent>
    </el-dialog>
  </div>
</template>

<script>
import { listPatent, getPatent, delPatent, addPatent, updatePatent } from "@/api/work/patent";
import PatentDetail from "@/views/work/patent/Detail/index.vue";
import {listPatentWinners} from "@/api/work/patentWinners";
import {getUserProfile} from "@/api/system/user";
import AddPatent from "@/views/work/patent/AddPatent/index.vue";

export default {
  name: "Patent",
  components: {AddPatent, PatentDetail},
  dicts: ['sys_user_sex','author_type','school_signature','work_unit','work_ategory','patent_place_of_publication', 'school_audit_status', 'patent_is_call_for_language', 'subject_category', 'patent_publisher_level', 'faculty_audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,

      patentId: null,

      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWorkPatentWinners: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //
      patentWinnersList: [],
      // 著作
      patentList: [],
      // 著作完成人
      workPatentWinnersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      openClick: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patentName: null,
        affiliatedUnit: null,
        publicationTime: null,
        workCategory: null,
        publisher: null,
        auditStatus: null,
        placeOfPublication: null,
        subjectCategoryId: null,
        firstSubject: null,
        projectSource: null,
        patentWinnersId: null,
        totalBlockCount: null,
        isCallForLanguage: null,
        schoolSignature: null,
        language: null,
        isbnNumber: null,
        cipNumber: null,
        publisherLevel: null,
        filesUrl: null,
        dataAddUserId: null,
        dataAddDate: null,
        dataSchoolAuditUserId: null,
        dataSchoolAuditDate: null,
        dataSchoolAuditStatus: null,
        dataFacultyAuditUserId: null,
        dataFacultyAuditDate: null,
        dataFacultyAuditStatus: null,
        userId:null
      },
      // 表单参数
      form: {},
      form1: {
        filesUrl: null,
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.form1.filesUrl = this.queryParams.filesUrl;
    this.getList();

  },
  methods: {
    /** 查询著作列表 */
    async getList() {
      this.loading = true;
      //获取当前登录用户id并添加筛选
      console.log(userId)
      let userId=(await getUserProfile()).data.userId

      this.queryParams.userId = userId;
      listPatent(this.queryParams).then(async response => {
        this.patentList = response.rows;
        console.log(this.patentList,"1")
        this.patentWinnersList = (await listPatentWinners()).rows;
        for (const item of this.patentList) {
          for (const item2  of this.patentWinnersList  ) {
            if(item.id === item2.workPatentId && item2.signatureOrder=='1') {
              item.authorId = item2.authorName;
            }
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /**addPatent关闭*/
    handleClose(){
      this.open = false;
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        patentName: null,
        affiliatedUnit: null,
        publicationTime: null,
        workCategory: null,
        publisher: null,
        auditStatus: null,
        placeOfPublication: null,
        subjectCategoryId: null,
        firstSubject: null,
        projectSource: null,
        patentWinnersId: null,
        totalBlockCount: null,
        isCallForLanguage: null,
        schoolSignature: null,
        language: null,
        isbnNumber: null,
        cipNumber: null,
        publisherLevel: null,
        filesUrl: null,
        remark: null,
        dataAddUserId: null,
        dataAddDate: null,
        dataSchoolAuditUserId: null,
        dataSchoolAuditDate: null,
        dataSchoolAuditStatus: null,
        dataFacultyAuditUserId: null,
        dataFacultyAuditDate: null,
        dataFacultyAuditStatus: null,
        userId:null
      };
      this.workPatentWinnersList = [];
      this.resetForm("form");
      this.patentId = null;
    },
    /** 著作详情链接点击事件 */
    clickDetail(row) {
      this.openDetail = true;
      this.patentId = row.id;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.openClick = true;
      this.title = "添加著作";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPatent(id).then(response => {
        this.form = response.data;
        this.workPatentWinnersList = response.data.workPatentWinnersList;
        this.open = true;
        this.openClick = false;
        this.title = "修改著作";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.workPatentWinnersList = this.workPatentWinnersList;
          if (this.form.id != null) {
            updatePatent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPatent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除著作编号为"' + ids + '"的数据项？').then(function() {
        return delPatent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 著作完成人
序号 */
    rowWorkPatentWinnersIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 著作完成人
添加按钮操作 */
    handleAddWorkPatentWinners() {
      let obj = {};
      obj.signatureOrder = "";
      obj.authorType = "";
      obj.authorName = "";
      obj.gender = "";
      obj.educationBackgorund = "";
      obj.affiliatedUnit = "";
      obj.contributionRate = "";
      obj.professionalTitle = "";
      obj.role = "";
      obj.coeditNumber = "";
      this.workPatentWinnersList.push(obj);
    },
    /** 著作完成人删除按钮操作 */
    handleDeleteWorkPatentWinners() {
      if (this.checkedWorkPatentWinners.length == 0) {
        this.$modal.msgError("请先选择要删除的著作完成人数据");
      } else {
        const workPatentWinnersList = this.workPatentWinnersList;
        const checkedWorkPatentWinners = this.checkedWorkPatentWinners;
        this.workPatentWinnersList = workPatentWinnersList.filter(function(item) {
          return checkedWorkPatentWinners.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWorkPatentWinnersSelectionChange(selection) {
      this.checkedWorkPatentWinners = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/patent/export', {
        ...this.queryParams
      }, `patent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="css">
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: left;
  line-height: 60px;
  font-weight: bold;
}
.body {
  width: 100%;
  height: 1500px;
  margin-bottom: 20px;
}
.jbxx {
  width: 100%;
  height: 650px;
}
</style>
